﻿$.download = function (url, data, method) {
    if (url && data) {
        data = typeof data == 'string' ? data : jQuery.param(data);
        var inputs = '';
        $.each(data.split('&'), function () {
            var pair = this.split('=');
            inputs += '<input type="hidden" name="' + pair[0] + '" value="' + pair[1] + '" />';
        });
        $('<form action="' + url + '" method="' + (method || 'post') + '">' + inputs + '</form>').appendTo('body').submit().remove();
    };
};

function getGridHeight() {
    var headerHeight = top.$('#eq-header').height();
    var tabHeight = top.$('#eq-manage-tab').height();
    var operateHeight = $('#xd-appbar').height();
    return $(window).height() -  tabHeight - operateHeight-16;
}

function getBoolText(bool) {
    if (bool)
        return '<span class="mdui-text-color-theme">是</span>';
    return '<span class="mdui-text-color-theme-accent">否</span>';
};

function getBoolIcon(bool) {
    if (bool)
        return '<span class="mdui-text-color-theme"><i class="mdui-icon material-icons">check</i></span>';
    return '<span class="mdui-text-color-theme-accent"><i class="mdui-icon material-icons">close</i></span>';
};

$.fn.organizeDataGrid = function (option) {
    var defaults = {
        datatype: "json",
        autowidth: true,
        rownumbers: true,
        shrinkToFit: false,
        gridview: true,
        styleUI: 'Bootstrap4',
        iconSet: 'fontAwesome'
    };
    var options = $.extend(defaults, option);
    var $element = $(this);

    options["loadComplete"] = function () {
        $('.eq-operation').hide();
    };

    options["onSelectRow"] = function (rowid, status, e) {
        var length = $(this).jqGrid("getGridParam", "selrow").length;
        if (length > 0) {
            $(this).find('tr').removeClass('mdui-color-theme-100');
            $(this).find('tr.table-success').addClass('mdui-color-theme-100');
            var rowData = $(this).jqGrid('getRowData', rowid);
            var nodeType = rowData.NodeType;
            
            if (nodeType === "组织") {
                $('.eq-operation').hide();
                $('#eq-operate-organize').show();
            }
            else if (nodeType === "岗位") {
                $('.eq-operation').hide();
                $('#eq-operate-post').show();
            }
            else if (nodeType === "人员") {
                $('.eq-operation').hide();
                $('#eq-operate-user').show();
            }
            
        } else {
            $('.eq-operation').hide();
            $(this).find('tr').removeClass('mdui-color-theme-100');
        }
        $('.eq-operate-cancel-select').bind('click', function () {
            $('.eq-operation').hide();
            $element.jqGrid('resetSelection');
            $(this).find('tr').removeClass('mdui-color-theme-100');
        });

    };
    options['onUnSelectRow'] = function (rowid) {
        $(this).find('tr').removeClass('mdui-color-theme-100');
    };
    $element.jqGrid(options);
};


$.fn.dataGrid = function (option) {
    var defaults = {
        datatype: "json",
        autowidth: true,
        rownumbers: true,
        shrinkToFit: false,
        gridview: true,
        styleUI: 'Bootstrap4',
        iconSet: 'fontAwesome'
    };
    var options = $.extend(defaults, option);
    var $element = $(this);

    options["loadComplete"] = function () {
        $('#eq-operate').hide();
    };

    options["onSelectRow"] = function (rowid, status, e) {
        
        var length = $(this).jqGrid("getGridParam", "selrow").length;
        if (length > 0) {
            $('#eq-operate').show();
            $(this).find('tr').removeClass('mdui-color-theme-100');
            $(this).find('tr.table-success').addClass('mdui-color-theme-100');
        } else {
            $('#eq-operate').hide();
            $(this).find('tr').removeClass('mdui-color-theme-100');
        }
        $('.eq-operate-cancel-select').bind('click', function () {
            $('#eq-operate').hide();
            $element.jqGrid('resetSelection');
            $(this).find('tr').removeClass('mdui-color-theme-100');
        });
        
    };
    options['onUnSelectRow'] = function (rowid) {
        $(this).find('tr').removeClass('mdui-color-theme-100');
    };
    $element.jqGrid(options);
};

$.fn.jqGridRowValue = function () {
    var $grid = $(this);
    var selectedRowIds = $grid.jqGrid("getGridParam", "selarrrow");
    if (selectedRowIds != "") {
        var json = [];
        var len = selectedRowIds.length;
        for (var i = 0; i < len; i++) {
            var rowData = $grid.jqGrid('getRowData', selectedRowIds[i]);
            json.push(rowData);
        }
        return json;
    } else {
        return $grid.jqGrid('getRowData', $grid.jqGrid('getGridParam', 'selrow'));
    }
};

$.GetEnumText = function (cellValue, enumType) {
    var e = eval(enumType);
    for (var i = 0; i < e.length; i++) {
        if (cellValue == e[i].Value)
            return e[i].Text;
    }
    return '';
};

